﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace libReadWrite
{
    public abstract class BigEndianWriter
    {
        public abstract void Write(byte v);

        public abstract void Write(byte[] buffer);

        public abstract void Write(int v);

        public abstract void Write(long v);

        public abstract byte[] ConvertToBigEndianBytes(byte[] input);

        public void WriteString(String str)
        {
            if (str == null)
            {
                throw new Exception("The string is null.");
            }
            else
            {
                int nlength = str.Length;

                if (nlength == 0)
                {
                    this.Write(0);
                }
                else if (nlength > 0)
                {
                    byte[] unicode = System.Text.Encoding.Unicode.GetBytes(str);

                    this.Write(unicode.Length);
                    this.Write(unicode);
                }
            }
        }

        public void WriteByteArray(byte[] byteArray)
        {
            this.Write(byteArray.Length);
            this.Write(byteArray);
        }
    }
}
